<?php
/**
 * 水浒竞技场
 * Enter description here ...
 * @author Administrator
 *
 */
class Module_overlordcolosseum{
	/**
	 * 构造函数
	 */
	function __construct(){
	
	}
	/**
	 * 进入竞技场
	 * Enter description here ...
	 */
	function index(){
		$smarty = loadSmarty();
		$uin = SessionInfo('uin');
		loadLib('app_Overlord_Colosseum');
		loadLib('App_Overlord_Query');
		loadLib('App_Overlord_GameStart');
		//查看报名表
		$colosseum_info = app_Overlord_Colosseum::oneTranscript('app_overlord_apply', 'apply_' . $uin);
		if (!empty($colosseum_info['Value']['generals_id'])){
			$generals_info = App_Overlord_Query::oneUin_Generals($uin);
			
			if (!empty($generals_info['Value'][$colosseum_info['Value']['generals_id']])){
				$time = time();
				$row = $generals_info['Value'][$colosseum_info['Value']['generals_id']];
				//体力恢复
				if (!empty($row['physical_time'])){
					 if ($row['physical_time'] - $time < 0){
						$physical_time = 3600;
						//将领有恢复体力技能
						if (!empty($row['skills']['study'])){
							foreach ($row['skills']['study'] as $j => $skills_row){
								if ($j == 'train_hp'){
									//减短体力恢复时间
									$skills_num = $data['study'][$j][$skills_row]['num'];
									$physical_time -= intval($physical_time * $skills_num/100);
								}
							}
						}
					 	$num = intval(($time - $row['physical_time'])/$physical_time) + 1;
					 	$row['physical'] += $num;
						
					 	if ($row['physical'] < 10){
					 		$generals_info['Value'][$colosseum_info['Value']['generals_id']]['physical'] += $num;
					 		$end_time = ($time - $row['physical_time'])%$physical_time;
					 		$generals_info['Value'][$colosseum_info['Value']['generals_id']]['physical_time'] = $time + $physical_time - $end_time;
					 	}else{
					 		//满10或超过10点体力,体力都是满
					 		$generals_info['Value'][$colosseum_info['Value']['generals_id']]['physical'] = 10;
					 		//给将领恢复体力时间归0
					 		$generals_info['Value'][$colosseum_info['Value']['generals_id']]['physical_time'] = 0;
					 		//显示将领体力时的赋值
					 		$row['physical'] = 10;
					 	}
					 	//修改将领
						App_Overlord_Query::alterUin_generals($uin , $result['Value']);
					 }else{
					 	$row['physical_end_time'] = App_Overlord_GameStart::getEndDate($row['physical_time']);
					 }
				}
				$generals_lv = App_Overlord_GameStart::getGeneralsExp($row['lv']);
				if (!empty($generals_lv)){
					if ($row['exp'] >= $generals_lv['upgrade']){
						//将领升级
						$info = App_Overlord_GameStart::getGeneralsPromotion($generals_info['Value'][$colosseum_info['Value']['generals_id']], $generals_lv);
						$generals_info['Value'][$colosseum_info['Value']['generals_id']] = $info;
						
						//修改将领
						$code = App_Overlord_Query::alterUin_Generals($uin  , $generals_info['Value']);
						App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordcolosseum');
					}
					$smarty->assign('exp' , $generals_lv['upgrade']);
				}
				$generals_id_info = $generals_info['Value'][$colosseum_info['Value']['generals_id']];
				$smarty->assign('generals_info' , $generals_id_info);
			}
			$tower = array('码头'  , '青龙堂' , '白虎堂' , '清风堂' , '忠义堂' , '聚义堂');
			if (empty($colosseum_info['Value']['tower'])){
				$num_tower = 0;
			}else{
				$num_tower = $colosseum_info['Value']['tower'];
				if ($num_tower == 6){
					
				}
			}
			$smarty->assign('num' , $num_tower);
			$smarty->assign('tower' , $tower);
		}
		$smarty->assign('colosseum_info' , $colosseum_info);
		return $smarty->fetch('app/Overlord/colosseum.tpl.php');
	}
	/**
	 * 派遣武将
	 * Enter description here ...
	 */
	function dispatch(){
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$smarty = loadSmarty();
		$uin = SessionInfo('uin');
		loadLib('App_Overlord_Query');
		loadLib('App_Overlord_GameStart');
		$result = App_Overlord_Query::oneUin_Generals($uin);
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		if (!empty($result['Value'])){
			foreach ($result['Value'] as $k => $row){
			//体力恢复
				if (!empty($row['physical_time'])){
			
				 	if ($row['physical_time'] - $time < 0){
				 		$physical_time = 3600;
						//武将有恢复体力技能
						if (!empty($row['skills']['study'])){
							foreach ($row['skills']['study'] as $j => $skills_row){
								if ($j == 'train_hp'){
									//减短体力恢复时间
									$skills_num = $data['study'][$j][$skills_row]['num'];
									$physical_time -= intval($physical_time * $skills_num/100);
								}
							}
						}
					 	$num = intval(($time - $row['physical_time'])/$physical_time) + 1;
					 	$row['physical'] += $num;
					 	if ($row['physical'] < 10){
					 		$result['Value'][$k]['physical'] += $num;
					 		$end_time = ($time - $row['physical_time'])%$physical_time;
					 		$result['Value'][$k]['physical_time'] = $time + $physical_time - $end_time;
					 	}else{
					 		//满10或超过10点体力,体力都是满
					 		$result['Value'][$k]['physical'] = 10;
					 		//给武将恢复体力时间归0
					 		$result['Value'][$k]['physical_time'] = 0;
					 		//显示武将体力时的赋值
					 	}
					}
				}
				if ($row['state'] == 0){
					$row['id'] = $k;
					$list_array[] = $row;
				}
				
				//武将升级所需的经验
				$generals_lv = App_Overlord_GameStart::getGeneralsExp($row['lv']);
				
				if (!empty($generals_lv['upgrade'])){
					if ($row['exp'] >= $generals_lv['upgrade']){
						//武将升级
						$info = App_Overlord_GameStart::getGeneralsPromotion($result['Value'][$k], $generals_lv);
						$result['Value'][$k] = $info;
						$statelv = 1;
					}
				}
			}
		}
		if (!empty($num) || !empty($statelv)){
			//修改武将
			$code = App_Overlord_Query::alterUin_generals($uin , $result['Value']);
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordcolosseum&a=dispatch');
		}
		if (!empty($list_array)){
			$count = count($list_array);
			/*******************数据分页******************/
			$page = ($page-1) * $per_page;
			//武将等级从高到低排序
			$generals_max_lv = App_Overlord_GameStart::getGeneralsArrSort($list_array);
					
			$list = array_slice($generals_max_lv,$page,$per_page);		
			$smarty->assign('per_page',$per_page);
			$smarty->assign('count',$count);
			$smarty->assign('list' , $list);
		}	
		return $smarty->fetch('app/Overlord/c_dispatch.tpl.php');
	}
	/**
	 * 进入兵营调兵
	 */
	function assign(){
		$generals_id = isset($_GET['generals']) ? trim($_GET['generals']) : '';
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		$smarty = loadSmarty();
		
		$state_key = 'state_'.$uin;//练兵状态
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		loadLib('app_Overlord_Colosseum');
		
		// =================================== 查询兵营 ====================================
		$result = App_Overlord_Query::oneSoldiers($uin);

		if (empty($result)){
			$value = array(array('type' => 'infantry', 'count' => 500), array('type' => 'cavalryman', 'count' => 500), array('type' => 'toxotae', 'count' => 500), array('type' => 'special', 'count' => 500));
			App_Overlord_Query::addSoldiers($uin, $value);
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordcolosseum&a=assign&generals_id='.$generals_id);
			break;
		}
		//查询是否报名
		$colosseum_info = app_Overlord_Colosseum::oneTranscript('app_overlord_apply', 'apply_' . $uin);

		//查询个人武将表
		$generals_info = App_Overlord_Query::oneUin_Generals($uin);

		if (empty($generals_info['Value'])){
			return View::error('你还没有武将.');
		}
		if (empty($generals_info['Value'][$generals_id])){
			return View::error('该名武将不存在.');
		}
		//武将可带兵数
		$make_count = intval($generals_info['Value'][$generals_id]['make_count'] / 4);
		$subtract = 0;
		//剩余兵力
		$surplus_barracks = array();
		//初始可带兵
		$make_barracks = array();
		//分兵
		foreach ($result['Value'] as $k => $row){
			if ($row['count'] < $make_count){
				$make_barracks[$k] = $row['count'];
				$subtract = $make_count - $row['count'];
				//剩余兵力0
				$surplus_barracks[$k] = 0;
			}else{
				$make_barracks[$k] = $make_count;
				$surplus_barracks[$k] = $row['count'] - $make_count;
			}
			// 补兵
			if ($subtract > 0){
				foreach ($surplus_barracks as $k => $row){
					if ($row > 0){
						$temp_subtract = min($subtract , $row);
						$surplus_barracks[$k] -= $temp_subtract;
						$make_barracks[$k] += $temp_subtract;
						$subtract -= $temp_subtract;
					}
					if ($subtract <= 0) break;
				}
			}
		}

		$generals_count = App_Overlord_GameStart::getGeneralsBarracksLv($generals_info['Value'][$generals_id]['lv']);
		
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('君主ID不存在.');
		}
		$list = App_Overlord_GameStart::getBarracksType($user_info['Value']['country']);

		//初始兵力
		$barracks_count = 0;
		$is_list = App_Overlord_Query::oneState($uin);
		foreach ($list as $k => $row){
			
			$list[$k]['name'] = $row[$result['Value'][$k]['type']];
			$list[$k]['count'] = $result['Value'][$k]['count'];
			$list[$k]['type'] = $result['Value'][$k]['type'];
		}
		if (!empty($is_list)){
			if (!empty($is_list['Value']['train_barracks'])){ //有参数不让跳转
				foreach ($is_list['Value']['train_barracks'] as $k => $row){
					if ($row['time'] - $time <= 0){
						$tiem_list[$k] = $row;
						App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordcolosseum&a=assign&generals_id='.$generals_id);
					}else{
						$uin_li['train_barracks'][$k] = $is_list['Value']['train_barracks'][$k];
						$is_list['Value']['train_barracks'][$k]['end_time'] = App_Overlord_GameStart::getEndDate($row['time']);
					}
				}
			}
			if (!empty($tiem_list)){
				foreach ($result['Value'] as $k => $row){
					if (!empty($tiem_list[$row['type']])){
						$result['Value'][$k]['count'] += $tiem_list[$row['type']]['count'];  
					
					}
				}
				if (!empty($uin_li)){
					$rs['Value'] = $uin_li;
				} else{
					$rs['Value']['train_barracks'] = '';
				}
				$code = App_Overlord_Query::alterSoldiers($uin , $result['Value']);
				$code = App_Overlord_Query::alterState($uin , $rs['Value']);
			}
	
			$smarty->assign('is_list',$is_list['Value']['train_barracks']);
		}
		$smarty->assign('barracks_count',$barracks_count);
		//获取最少带兵数
		$min_generals_count = $generals_count['count'] * 0.1;
		$smarty->assign('make_barracks',$make_barracks);
		$smarty->assign('generals_info',$generals_info['Value'][$generals_id]);
		$smarty->assign('generals_id',$generals_id);
		$smarty->assign('min_generals_count',$min_generals_count);
		$smarty->assign('list',$list);
		$smarty->assign('colosseum_info',$colosseum_info);
		return $smarty->fetch('app/Overlord/assign.tpl.php');
	}
	/**
	 * 报名竞技场
	 * Enter description here ...
	 */
	function registration(){
		$type = isset($_POST['type']) ? trim($_POST['type']) : '';
		$generals_id = isset($_POST['generals']) ? trim($_POST['generals']) : '';
		$infantry = isset($_POST['infantry']) ? intval($_POST['infantry']) : null;
		$cavalryman = isset($_POST['cavalryman']) ? intval($_POST['cavalryman']) : null;
		$toxotae = isset($_POST['toxotae']) ? intval($_POST['toxotae']) : null;
		$special = isset($_POST['special']) ? intval($_POST['special']) : null;
		
		$uin = SessionInfo('uin');
		$smarty = loadSmarty();
		loadLib('App_Overlord_Query');
		loadLib('App_Overlord_GameStart');
		loadLib('app_Overlord_Colosseum');
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info) && empty($user_info['Value']['name'])) {
			return View::error('数据异常!');
		}
		if ($user_info['Value']['lv'] < 8){
			return View::error('你的等级未到16级,速去升级!' , 0 ,array(
					array('name' => '去做任务' , 'url' => '/app.php?m=overlordtask&amp;a=opentask&amp;sid=' . sessionInfo('skey')),
					array('name' => '去攻城' , 'url' => '/app.php?m=overlordcity&amp;a=battlecity&amp;sid=' . sessionInfo('skey')),
					array('name' => '返回游戏主页' , 'url' => '/app.php?m=overlorduser&amp;sid=' . sessionInfo('skey'))
				));
		}
		$colosseum_info = app_Overlord_Colosseum::oneTranscript('app_overlord_apply', 'apply_' . $uin);
		if (!empty($colosseum_info)){
			return View::error('你已报名!' , 0 ,array(
						array('name' => '返回竞技场' , 'url' => '/app.php?m=overlordcolosseum&amp;sid=' . sessionInfo('skey'))
					));
		}
		// 查询个人武将表
		$generals_info = App_Overlord_Query::oneUin_Generals($uin);
		if (empty($generals_info['Value']) || empty($generals_info['Value'][$generals_id])){
			return View::error('你没有该武将');
		}
		if ($generals_info['Value'][$generals_id]['state'] != 0){
			return View::error('该武将无法报名');
		}
		$id_general_info = $generals_info['Value'][$generals_id];	
		if ($id_general_info['physical'] < 4){
			return View::error('你的武将体力不足4点,无法报名');
		}
		$max_count = App_Overlord_GameStart::getGeneralsBarracksLv($id_general_info['lv']);
		//获取最少带兵数
		$min_generals_count = $max_count['count'] * 0.1;
		
		// 计算总兵数量
		$bing_count = $infantry + $cavalryman + $toxotae + $special;
		if ($bing_count < $min_generals_count){
			return View::error('你的武将至少带领'.$min_generals_count.'个士兵');
		}
		//数量超过最大
		if ($bing_count > $id_general_info['make_count']){
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordbarracks&a=fight_brr&generals='.$generals_id .'&type='.$type);
			break;
		}
		$value = array('name' => $user_info['Value']['name'] , 'generals_id' => $generals_id);
		//添加报名
		$code = app_Overlord_Colosseum::addTranscript('app_overlord_apply', 'apply_' . $uin, $value);
		if ($code == 0){
			//竞技报名状态
			$generals_info['Value'][$generals_id]['state'] = 8;
			$barracks_now_array = array($infantry , $cavalryman , $toxotae , $special);
			$generals_info['Value'][$generals_id]['make'] = $barracks_now_array;
			
			//修改武将报名状态
			$code = app_Overlord_Query::alterUin_Generals($uin, $generals_info['Value']);
			//跳转
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordcolosseum');
		}
	}
}
# end of script